let  server=require('express')();
let mysql=require('mysql');
const port=81;
server.all("*",function(req,res,next){
	    //设置允许跨域的域名，*代表允许任意域名跨域
	    res.header("Access-Control-Allow-Origin","*");
	    //允许的header类型
	    res.header("Access-Control-Allow-Headers","content-type");
	    //跨域允许的请求方式 
	    res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
	    if (req.method.toLowerCase() == 'options')
	        res.send(200);  //让options尝试请求快速结束
	    else
	        next();
	})
//数据库
let sql=mysql.createConnection({
	host:'localhost',
	user:'root',
	password:'root',
	database:'haiou'
});
//尝试连接
sql.connect();
//商品列表页数据===================================
server.get("/goodslist",function (request,response){
	sql.query(`select * from goodsnum`,function(error,data){
		//如果有错误
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.send(JSON.stringify(data));
	})
})
//y用户注册
server.get("/adduser", function(request, response) {
	//接受前面传来的姓名和密码
	let name = request.query.name;
	let pass = request.query.pass;
	//判断用户是否存在
	sql.query(`select * from stu where username="${name}"`, function(error,data) {
		//如果有错误
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		if(data.length===0){
			sql.query(`insert into stu(username,password) values ("${name}","${pass}")`,(error,result)=>{
				//如果有错误
				if (error) {
					console.log(error);
					response.end("error");
					return;
				}
				response.end("success");
			})
		}else{
			response.end("exist");
		}

	})
})
//用户登录
server.get("/selectstu",function(request,response){
	    //接收前面传来的数据
	  let name=request.query.name;
	  let pass=request.query.pass;
	  //查询数据库判断用户名或密码是否存在
	  sql.query(`select * from stu where username="${name}"and password="${pass}"`,function(error,data){
		    //如果有错误
		    if (error) {
		    	console.log("有错误");
		    	response.end("有错误");
		    	return;
		    }
			if(data.length===1){
				response.end("success");
			}else{
				response.end("exist");
			}
	  })
})
//=====双表查询=====================
server.get("/countgoods",function (request,response){
	let id = request.query.id;
	sql.query(`select * from goodsnum join  goodlist on goodsnum.id=goodlist.goodsid where goodsnum.id=${id}`,function(error,data){
		//如果有错误
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.send(JSON.stringify(data));
	})
})
//=====订单信息=====================
server.get("/dingdan",function (request,response){
	let username = request.query.username;
	let photo = request.query.photo;
	let youbian = request.query.youbian;
	let threecity = request.query.threecity;
	let hoset = request.query.hoset;
	let shuliang = request.query.shuliang;
	let huafei = request.query.huafei;
	let newstuname = request.query.newstuname;
	sql.query(`insert into photostu(username,photo,youbian,threecity,hoset,shuliang,huafei,gouname) values ("${username}","${photo}","${youbian}","${threecity}","${hoset}","${shuliang}","${huafei}","${newstuname}")`,(error,result)=>{
		//如果有错误
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		response.end("success");
	})
})
//=====个人信息列表=====================
server.get("/newonename",function (request,response){
	let goodsimg = request.query.limg;
	let goodname = request.query.newgoodname;
	let money = request.query.newnewmoney;
	let data = request.query.newdate;
	let gouname = request.query.gindname;
	
	sql.query(`insert into goumai(goodsimg,goodname,money,data,gouname) values ("${goodsimg}","${goodname}","${money}","${data}","${gouname}")`,(error,result)=>{
		//如果有错误
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		response.end("success");
	})
})
//=====查询浏览记录=====================
server.get("/oldlook",function (request,response){
	let oldusername = request.query.oldusername;
	sql.query(`select * from goumai where gouname="${oldusername}" `,function(error,data){
		//如果有错误
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.send(JSON.stringify(data));
	})
})
//购物车信息=========================
server.get("/newgouwuche",function (request,response){
	let goodsimg = request.query.limg;
	let goodname = request.query.newgoodname;
	let money = request.query.shuju;
	sql.query(`insert  into gouwuchelist(tupian,foodname,danjia) values ("${goodsimg}","${goodname}","${money}") `,function(error,data){
		//如果有错误
			if (error) {
				console.log(error);
				response.end("error");
				return;
			}
			response.end("success");
	})
})
//查询购物车=========================================
server.get("/selectnewgouwuche",function (request,response){
	sql.query(`select * from gouwuchelist `,function(error,data){
		//如果有错误
			if(error){
				console.log(error);
				response.end("error");
				return;
			}
			response.send(JSON.stringify(data));
		})
})
//删除购物车信息================================
server.get("/usersgoodsid",function (request,response){
	let Id = request.query.id;
	sql.query(`delete from gouwuchelist where Id=${Id} `,function(error,data){
		//如果有错误
			if(error){
				console.log(error);
				response.end("error");
				return;
			}
			response.end("success");
		})
})
//监听81端口
server.listen(port);
console.log("server is running at"+port);